package models

import (
	"time"
)

// Department 部门模型
type Department struct {
	ID          int64      `json:"id" gorm:"primaryKey;autoIncrement"`
	Name        string     `json:"name" gorm:"type:varchar(100);not null;comment:部门名称"`
	Code        string     `json:"code" gorm:"type:varchar(50);not null;uniqueIndex:uk_code;comment:部门编码"`
	Description *string    `json:"description" gorm:"type:text;comment:部门描述"`
	Status      string     `json:"status" gorm:"type:varchar(20);not null;default:active;index:idx_status;comment:状态"`
	CreatedBy   int64      `json:"created_by" gorm:"not null;index:idx_created_by;comment:创建人ID"`
	UpdatedBy   int64      `json:"updated_by" gorm:"not null;index:idx_updated_by;comment:更新人ID"`
	CreatedAt   time.Time  `json:"created_at" gorm:"not null;default:CURRENT_TIMESTAMP;comment:创建时间"`
	UpdatedAt   time.Time  `json:"updated_at" gorm:"not null;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;comment:更新时间"`
	DeletedAt   *time.Time `json:"deleted_at" gorm:"index:idx_deleted_at;comment:删除时间"`

	// 关联字段
	CreatedByUser *User            `json:"created_by_user" gorm:"foreignKey:CreatedBy"`
	UpdatedByUser *User            `json:"updated_by_user" gorm:"foreignKey:UpdatedBy"`
	Users         []DepartmentUser `json:"users" gorm:"foreignKey:DepartmentID"`
	UserCount     int64            `json:"user_count" gorm:"-"`
}

// TableName 指定表名
func (Department) TableName() string {
	return "departments"
}

// DepartmentUser 部门用户关联模型
type DepartmentUser struct {
	ID           int64      `json:"id" gorm:"primaryKey;autoIncrement"`
	DepartmentID int64      `json:"department_id" gorm:"not null;index:idx_department_id;comment:部门ID"`
	UserID       int64      `json:"user_id" gorm:"not null;index:idx_user_id;comment:用户ID"`
	JoinDate     time.Time  `json:"join_date" gorm:"type:date;not null;index:idx_join_date;comment:加入部门日期"`
	LeaveDate    *time.Time `json:"leave_date" gorm:"type:date;index:idx_leave_date;comment:离开部门日期"`
	Status       string     `json:"status" gorm:"type:varchar(20);not null;default:active;index:idx_status;comment:状态"`
	CreatedBy    int64      `json:"created_by" gorm:"not null;index:idx_created_by;comment:创建人ID"`
	UpdatedBy    int64      `json:"updated_by" gorm:"not null;index:idx_updated_by;comment:更新人ID"`
	CreatedAt    time.Time  `json:"created_at" gorm:"not null;default:CURRENT_TIMESTAMP;comment:创建时间"`
	UpdatedAt    time.Time  `json:"updated_at" gorm:"not null;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;comment:更新时间"`

	// 关联字段
	Department    *Department `json:"department" gorm:"foreignKey:DepartmentID"`
	User          *User       `json:"user" gorm:"foreignKey:UserID"`
	CreatedByUser *User       `json:"created_by_user" gorm:"foreignKey:CreatedBy"`
	UpdatedByUser *User       `json:"updated_by_user" gorm:"foreignKey:UpdatedBy"`
}

// TableName 指定表名
func (DepartmentUser) TableName() string {
	return "department_users"
}

// DepartmentCreateRequest 创建部门请求
type DepartmentCreateRequest struct {
	Name        string `json:"name" binding:"required" validate:"required"`
	Code        string `json:"code" binding:"required" validate:"required"`
	Description string `json:"description"`
}

// DepartmentUpdateRequest 更新部门请求
type DepartmentUpdateRequest struct {
	Name        string `json:"name" binding:"required" validate:"required"`
	Code        string `json:"code" binding:"required" validate:"required"`
	Description string `json:"description"`
	Status      string `json:"status" binding:"required" validate:"required,oneof=active inactive deleted"`
}

// DepartmentUserCreateRequest 创建部门用户关联请求
type DepartmentUserCreateRequest struct {
	DepartmentID int64  `json:"department_id" binding:"required" validate:"required"`
	UserID       int64  `json:"user_id" binding:"required" validate:"required"`
	JoinDate     string `json:"join_date"`                                                   // 可选，默认为当前日期
	Status       string `json:"status" validate:"omitempty,oneof=active inactive suspended"` // 可选，默认为active
}

// DepartmentUserUpdateRequest 更新部门用户关联请求
type DepartmentUserUpdateRequest struct {
	JoinDate  string `json:"join_date" binding:"required" validate:"required"`
	LeaveDate string `json:"leave_date"`
	Status    string `json:"status" binding:"required" validate:"required,oneof=active inactive suspended"`
}

// DepartmentListRequest 部门列表请求
type DepartmentListRequest struct {
	Page     int    `form:"page" binding:"required,min=1"`
	PageSize int    `form:"page_size" binding:"required,min=1,max=100"`
	Name     string `form:"name"`
	Code     string `form:"code"`
	Status   string `form:"status"`
}

// DepartmentUserListRequest 部门用户列表请求
type DepartmentUserListRequest struct {
	Page         int    `form:"page" binding:"required,min=1"`
	PageSize     int    `form:"page_size" binding:"required,min=1,max=100"`
	DepartmentID int64  `form:"department_id"`
	UserID       int64  `form:"user_id"`
	Status       string `form:"status"`
}

// DepartmentUserTransferRequest 用户更换部门请求
type DepartmentUserTransferRequest struct {
	UserID          int64 `json:"user_id" binding:"required" validate:"required"`
	NewDepartmentID int64 `json:"new_department_id" binding:"required" validate:"required"`
}
